from bases.db import get_model
from bases.view import permission, siwa, UserAPI, rscode_response, RsCode, create_log
from models.model import Permission
from schemas.auth_permission import PermissionCreateSchema, PermissionPaginationParams, PermissionPaginationSchema, \
    PermissionUpdateSchema, PermissionItemSchema
from services.auth_permission import PermissionService


class PermissionListView(UserAPI):
    @permission("permission.add")
    @siwa.doc(
        body=PermissionCreateSchema,
        description="创建权限",
        group="auth",
        tags=["创建权限"],
    )
    def post(self, body: PermissionCreateSchema):
        model = get_model(Permission)
        record = model.query.filter(model.name == body.name).scalar()
        if record:
            return rscode_response(RsCode.ARGS_ERROR, new_msg="权限名称已存在")
        record = model.query.filter(model.value == body.value).scalar()
        if record:
            return rscode_response(RsCode.ARGS_ERROR, new_msg="权限值已存在")
        is_created = PermissionService.create(body)
        if not is_created:
            return rscode_response(RsCode.OTHER_ERROR)
        create_log("新增权限")
        return rscode_response()

    @permission("permission.page")
    @siwa.doc(
        query=PermissionPaginationParams,
        resp=PermissionPaginationSchema,
        description="权限分页",
        group="auth",
        tags=["权限分页"],
    )
    def get(self, query: PermissionPaginationParams):
        page_query = PermissionService.page_query(query_params=query)
        page_list = PermissionService.patch_page_query(page_query)
        page_data = PermissionPaginationSchema(
            data=page_list, total=page_query.total, pageSize=query.pageSize
        ).dict()
        return rscode_response(data=page_data)


class PermissionDetailView(UserAPI):

    @permission("permission.update")
    @siwa.doc(
        body=PermissionUpdateSchema,
        description="编辑权限",
        group="auth",
        tags=["编辑权限"]
    )
    def put(self, id, body: PermissionUpdateSchema):
        model = get_model(Permission)
        record = model.query.filter(model.id == id).scalar()
        if not record:
            return rscode_response(RsCode.ARGS_ERROR, new_msg="id不存在")
        db_record = model.query.filter(model.name == body.name, model.id != id).scalar()
        if db_record:
            return rscode_response(RsCode.ARGS_ERROR, new_msg="名称不可重复")
        db_record = model.query.filter(model.value == body.value, model.id != id).scalar()
        if db_record:
            return rscode_response(RsCode.ARGS_ERROR, new_msg="值不可重复")
        is_updated = PermissionService.update(record, body)
        if not is_updated:
            return rscode_response(RsCode.OTHER_ERROR)
        create_log("编辑权限")
        return rscode_response()

    @permission("permission.delete")
    @siwa.doc(description="删除权限", group="auth", tags=["删除权限"])
    def delete(self, id):
        model = get_model(Permission)
        db_record = model.query.filter(model.id == id).scalar()
        if not db_record:
            return rscode_response(RsCode.ARGS_ERROR, new_msg="id不存在")
        is_deleted = PermissionService.delete(db_record)
        if not is_deleted:
            return rscode_response(RsCode.OTHER_ERROR)
        create_log("删除权限")
        return rscode_response()

    #@permission("permission.read")
    @siwa.doc(description="查询权限", group="auth", tags=["查询权限"])
    def get(self, id):
        model = get_model(Permission)
        db_record = model.query.filter(model.id == id).scalar()
        if not db_record:
            return rscode_response(RsCode.ARGS_ERROR, new_msg="id不存在")
        data = PermissionItemSchema(**db_record.to_dict()).dict()
        return rscode_response(data=data)
